package yz.cp.back.auth.oauth.userDetail;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import yz.cp.back.auth.security.SecUser;
import yz.cp.back.s.entity.SysUser;
import yz.cp.back.s.service.ISysUserService;

import java.util.Map;

@Slf4j
public class OauthUserDetailsService implements UserDetailsService {
    @Autowired
    private ISysUserService userService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        SysUser user = userService.findByUsername(s);
        if (user == null) {
            throw new InternalAuthenticationServiceException("登录失败");
        }
        SecUser secUse= new SecUser(user.getId(), user.getUsername(), user.getPassword(),
                Lists.newArrayList(), true,
                true, true,
                true).checkPass(true);
        Map map= Maps.newHashMap();
        map.put("roles",user.getRoles());
        map.put("permissions",user.getPermissions());
        secUse.setData(map);
        return secUse;
    }
}
